Qt.include("sha1.js")
Qt.include("oauth.js")
Qt.include("dbmanager.js")

var twitter = {}
twitter.api_root = "http://api.twitter.com";
twitter.api_version = "1";
twitter.statuses = {}
twitter.statuses.update_url = twitter.api_root + "/" + twitter.api_version + "/statuses/update.xml"
var oauth_data = { }
oauth_data.consumerKey = "eqE0YFNDqJZGBi3wjgYTgQ";
oauth_data.consumerSecret = "O5U1Sfi3WcoAfILWotEdZQT5sJF5YBOIqFSSSfhxSdA";
oauth_data.callback_url = "";
oauth_data.requestUrl="http://api.twitter.com/oauth/request_token"
oauth_data.accessUrl= "https://api.twitter.com/oauth/access_token"
oauth_data.authorizeUrl= "http://api.twitter.com/oauth/authorize"

function postToTwitter(message) {
    var xhr = new XMLHttpRequest();

    xhr.onreadystatechange = function() {
        if(xhr.readyState == XMLHttpRequest.DONE) {
            switch(xhr.status) {
            case 200:
                // successfull
                var text = "Twitter update succesfull";
                if(debug) log(text);
                showNotification(text);
                break;
            case 401:
                text = "Unauthorized access...Please link to your twitter account again.";
                if(debug) log(text);
                showNotification(text);
                break;
            default:
                // other error
                text = "Twitter post failed.";
                if(debug) log(text);
                showNotification(text);
                break;
            }
        }
    }

    var url = twitter.statuses.update_url;
    var parameters = { status: message }

    var header = getAuthorizationHeader(url, "POST", parameters);
    if(debug) log("header: "  +header);
    url += "?status=" + parameters.status;
    xhr.open("POST", url);
    xhr.setRequestHeader("Authorization", header)
    xhr.send();
}

function log(message){
    console.log(message);
}

function getAuthorizationHeader(url, method, parameters) {
    if(debug){
        log("window.oauth_token:" + window.oauth_token);
        log("window.oauth_token_secret:" + window.oauth_token_secret);
        log("oauth_data.consumerKey:" + oauth_data.consumerKey);
        log("oauth_data.consumerSecret:" + oauth_data.consumerSecret);
    }

    var accessor = {
        token: window.oauth_token,
        tokenSecret: window.oauth_token_secret,
        consumerKey : oauth_data.consumerKey,
        consumerSecret: oauth_data.consumerSecret
};

    var message = {
        "action": url,
        "method": method,
        "parameters": parameters
};

    OAuth.completeRequest(message, accessor);
    return OAuth.getAuthorizationHeader(window.oauth_token, parameters);    // no realm?
}




function requestPermanentAcccesToken(oauth_verifier) {

    var url = oauth_data.accessUrl;
    if(debug) console.log("accessUrl: " + url);

    var accessor = {
        token: window.oauth_token,
        tokenSecret: window.oauth_token_secret,
        consumerKey : oauth_data.consumerKey,
        consumerSecret: oauth_data.consumerSecret
};

    var message = {
        action: url,
        method: "POST",
        parameters: {
        auth_token: window.oauth_token,
        oauth_verifier: oauth_verifier
}
};

    OAuth.completeRequest(message, accessor);
    url = url + '?' + OAuth.formEncode(message.parameters);
    var doc = new XMLHttpRequest();
    doc.onreadystatechange = function() {
        if (doc.readyState == XMLHttpRequest.DONE) {
            if(doc.responseText != ""){
                var list = OAuth.decodeForm(doc.responseText);

                if(oauth_data.aDebug){
                    console.log("doc.responseText: " + doc.responseText);
                    console.log("list: " + list);
                    console.log("list.count: " + list.length);
                }

                if(list.length != 0) {
                    window.oauth_token = list[0][1];
                    window.oauth_token_secret = list[1][1];
                    window.user_id = list[2][1];
                    window.screen_name = list[3][1];

                    saveToken(window.screen_name, window.oauth_token, window.oauth_token_secret);

                    if(oauth_data.aDebug){
                        console.log("window.oauth_token: " + window.oauth_token);
                        console.log("window.oauth_token_secret: " + window.oauth_token_secret);
                        console.log("window.user_id: " + oauth_data.user_id);
                        console.log("window.screen_name: " + oauth_data.screen_name);
                    }
                }
            }
        }
    }

    doc.open("POST", url);
    doc.send();
}



function requestToken() {
    var url = oauth_data.requestUrl;
    if(debug){
        console.log("requestToken-url:" + url);
        console.log("oauth_data.consumer_key:" + oauth_data.consumerKey);
        console.log("oauth_data.api_secret:" + oauth_data.consumerSecret);
    }

    var accessor = {
        consumerKey : oauth_data.consumerKey,
        consumerSecret: oauth_data.consumerSecret
};

    var message = {
        action: url,
        method: "POST",
        parameters: {
        oauth_callback: oauth_data.callback_url
}
};

    OAuth.completeRequest(message, accessor);
    OAuth.SignatureMethod.sign(message, accessor);
    url = url + '?' + OAuth.formEncode(message.parameters);

    var doc = new XMLHttpRequest();
    doc.onreadystatechange = function() {
        if (doc.readyState == XMLHttpRequest.DONE) {
            var list = OAuth.decodeForm(doc.responseText);
            if(list.length !== 0) {
                window.oauth_token = list[0][1];
                window.oauth_token_secret = list[1][1];
                oauth_data.oauth_callback_confirmed = list[2][1];

                if(oauth_data.debug) {
                    console.log("\t window.oauth_token: " + window.oauth_token);
                    console.log("\t window.oauth_token_secret: " + window.oauth_token_secret);
                    console.log("\t oauth_data.oauth_callback_confirmed: " + oauth_data.oauth_callback_confirmed);
                }
                oauth_data.url = oauth_data.authorizeUrl + "?oauth_token=" + window.oauth_token;

            }
            else{
                console.log("! Fatal error, unable to get request token");
            }
        }
    }

    doc.open("POST", url);
    var header = OAuth.getAuthorizationHeader(oauth_data.oauth_realm, message.parameters);
    if(debug) console.log("header: " + header);
    doc.send();

}
